<?php

namespace App\Http\Controllers\Collect;

use App\Service\Collect\Xueqiu;


class XueqiuController extends BaseController
{
    // 雪球采集
    public function initialize()
    {
        parent::initialize();
    }


    // 采集并保存到数据库
    public function index()
    {
        // 采集
        $keyword = '易见股份';
        $collect = new Xueqiu();

        $page = 1;
        $size = 10;
        $list = $collect->getApiList($keyword, $page, $size);
        if(empty($list)){
            if($collect->getCode() == 500){
                return $this->error($collect->getError());
            } else{
                return $this->success([], '没有采集数据！');
            }
        }


        // 展示
        //$this->show($list[0]);

        // 采集详情内容
        //   TODO 这里采集的是百度的内容
        foreach($list as $key => $item){
            // 提示
            $this->showMsg(' '.($key + 1).'：==> 采集文章：'.$item['title']);

            $res = $collect->saveApiDataToDb($item, $key);
            if($res === false){
                if($collect->getCode() == 500){
                    return $this->error($collect->getError());
                } else{
                    $this->showMsg($collect->getError());
                }
            }
        }

        return $this->success([], '采集完成');
    }


}


